package system;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.StringTokenizer;

import ui.MainMenuUI;

public class StopWordsRemoval extends Algorithm {

    public static Set<String> stopWords;
    public static Set<String> tokens;
	/**
	 * constructor
	 */
	public StopWordsRemoval(){
		
		stopWords = new LinkedHashSet<String>();
		tokens = new LinkedHashSet<String>();
		
		BufferedReader br;
		
		try{	
		br = new BufferedReader(new FileReader("stop-words.txt"));
			for(String line;(line = br.readLine()) != null;)
			   stopWords.add(line.trim());
			
			br.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Read line from stop words file failed! ");
		}
		
		
	}
	
	
	
	
	
	
	
	public ArrayList<String>  run(ArrayList<String> pathes) {
		
		BufferedReader br;
		StringTokenizer st;
		String str;
		BufferedWriter out;
		ArrayList<String> output=new ArrayList<String>();
		double temp;
		MainMenuUI.lblRunStatus.setText("STOP WORDS REMOVAL IN PROGRESS...");
		MainMenuUI.progressBar.setValue(0);
		

		MainMenuUI.ChangeProgressBar();
		
		try {
			for(int i=0;i<pathes.size();i++){
				
				br = new BufferedReader(new FileReader(pathes.get(i)));
				out = new BufferedWriter(new FileWriter("out/outSWR"+i+".txt"));
				output.add("out/outSWR"+i+".txt");

			  for(String line;(line = br.readLine()) != null;){
				st = new StringTokenizer(line, ".,! ()[]{};:*`-+&^%$#@!~'?/<>12345\"6\\7890=");
				while (st.hasMoreElements()) {
					str=((String)st.nextElement()).toLowerCase();
					if(!stopWords.contains(str)){
					   // it's not a stop word
						//System.out.println(str);
						
						try{
							  
							 
							  out.write(str);
							  out.newLine();
							  //Close the output stream
							 
							  }catch (Exception e){//Catch exception if any
							  System.out.println("Unsecsessfully writing to book-out-stopwords txt file!");
							  }

					
				 }//end if
		   }//end while
				temp=(i+1/pathes.size())*100;
				MainMenuUI.progressBar.setValue((int)temp);
				

				MainMenuUI.ChangeProgressBar();
	    		
		}//end for1
			
			br.close();
			out.close();
			
	}//for
		

		} //end try
		catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Unsecsessfully tokenizing!");
		}
		// TODO Auto-generated method stub
		
		MainMenuUI.lblRunStatus.setText("STOP WORDS REMOVAL COMPLITED!");
		return output;
	
	}


		
	







}
